<?php
include APP_ROOT.'/inc/db.inc.php';
class tm_db
{
	public $adoconnection;
	
	function &getInstance($dbserver){
        static $instances = array();
        if(!array_key_exists($dbserver,$instances)){
            $instances[$dbserver] = new tm_db($dbserver);
        }
        $instance = $instances[$dbserver];
        return $instance;
    }
	
	private function __construct($dbserver){
		global $hosts;
		$this->adoconnection=&ADONewConnection('mysql');
		$this->adoconnection->debug = false;
		//$this->adoconnection->Connect(DEFAULT_DB_SERVER,DEFAULT_DB_USERNAME,DEFAULT_DB_PWD,DEFAULT_DB_SCHEMA);
		if($dbserver){
			$this->adoconnection->Connect($hosts[$dbserver][DB_SERVER],$hosts[$dbserver][DB_USERNAME],$hosts[$dbserver][DB_PWD],$hosts[$dbserver][DB_SCHEMA]);
		}else{
			$this->adoconnection->Connect(DEFAULT_DB_SERVER,DEFAULT_DB_USERNAME,DEFAULT_DB_PWD,DEFAULT_DB_SCHEMA);
		}
		$this->adoconnection->Execute("SET NAMES 'utf8'");
	}
	
	function __destruct(){
		$this->close();
	}
	
	function close(){
		$this->adoconnection->Close();
	}
	
	function insert($sql){
		$this->adoconnection->Execute($sql);
	}
	
	function insert_id(){
		return (int)$this->adoconnection->Insert_ID();
	}
	
	function update($sql){
		$this->adoconnection->Execute($sql);
		if($this->adoconnection->Affected_Rows())
			return true;
		else
			return false;
		
	}
	
	function delete($sql){
		$this->adoconnection->Execute($sql);
		if($this->adoconnection->Affected_Rows())
			return true;
		else
			return false;
	}
	
	function select($sql,$cache=0){
		if($cache){
			$rs=$this->adoconnection->CacheExecute($cache,$sql);	
		}else{
			$rs=$this->adoconnection->Execute($sql);
		}
		return $rs->GetRows();
	}
	
	function select_count($sql){
		$rs=$this->adoconnection->Execute($sql);
		return intval($rs->fields['count(*)']);
	}
	
	function select_one($sql){
		$rs=$this->adoconnection->Execute($sql);
		return $rs->fields;
	}
}
?>